package com.boot.app.system.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.api.ApiResult;
import com.boot.app.system.domain.SystemLog;
import com.boot.app.system.service.ISystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("system/log")
public class SystemLogController {

    private final ISystemLogService service;

    @Autowired
    public SystemLogController(ISystemLogService service){
        this.service = service;
    }

    @GetMapping("{id}")
    public ApiResult<SystemLog> getLog(@PathVariable("id") long id){
        SystemLog systemLog = service.getById(id);
        return ApiResult.success(systemLog);
    }

    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<SystemLog>> getLogPage(@PathVariable("page")int page, @PathVariable("size")int size, SystemLog log){
        LambdaQueryWrapper<SystemLog> wrapper = new LambdaQueryWrapper<>();
        if(NumberUtil.isValidNumber(log.getId())) wrapper.eq(SystemLog::getId, log.getId());
        if(NumberUtil.isValidNumber(log.getBizId())) wrapper.eq(SystemLog::getBizId, log.getBizId());
        if(StrUtil.isNotBlank(log.getType())) wrapper.eq(SystemLog::getType, log.getType());
        wrapper.orderByDesc(SystemLog::getGmtCreate);
        Page<SystemLog> logPage = service.page(new Page<>(page,size),wrapper);
        return ApiResult.success(logPage);
    }

    @DeleteMapping("{id}")
    public ApiResult<?> deleteLog(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

}
